<?php
//if its going to need the database then its probably smart to include it before we start 
require_once('database.php');
class User
{
    public $id;
    public $username;
    public $password;
    public $firstname;
    public $lastname;
    
    
    public static function find_all()
    {        
        $sql="SELECT * FROM users";
        $result_set=self::find_by_sql($sql);
        return $result_set;
    }
    
    public static function find_by_id($id=0)
    {
        global $database;
        $sql="SELECT * FROM users WHERE USER_ID={$id}";
        $result_set=self::find_by_sql($sql);
        $found=$database->fetch_array($result_set);
        return $found;
    }
    
    public static function find_by_sql($sql="")
    {
        global $database;
        $result_set=$database->query($sql);
        return $result_set;
    }
    
    public function full_name()
    {
        if(isset($this->firstname)&& isset($this->lastname))
        {
            return $this->firstname . " ". $this->lastname;
        }
        else
        {
            return "";
        }
    }
    
    private static function instantiate($record)
    {
//        $object = new self;
//        $object->id=$record['id'];
//        $object->username=$record['username'];
//        $object->password=$record['password'];
//        $object->firstname=$record['first_name'];
//        $object->lastname=$record['last_name'];
        
        foreach($record as $attribute=>$value)
        {
            if($object->has_attribute($attribute))
            {
                $object->$attribute=$value;
            }
        }
        
        return $object;
    }
    
    private function has_attribute($attribute)
    {
        //get_object_vars returns an associative array with all attributes including private ones!
        //as the keys and their current values as the value
        $object_vars = get_object_vars($this);
        //we dont care about the value we just want to know if the key exists
        //will return true or false
        return array_key_exists($attribute, $object_vars);
    }
    
}
?>
